package edu.bmstu.rslab;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.util.ArrayList;

/**
 * Created by Chikh (using IntelliJ IDEA).
* Date: 04.05.2009
* Time: 18:44:54
*/
class SetCSAction extends AbstractAction {
    private final VideoPanel videoPanel;
    private final Action setRobotPositionAction;
    private final Camera camera;
    private BasisOfLCS lcsBasis;
    private Translator translator;

    public SetCSAction(VideoPanel videoPanel, Action setRobotPositionAction, Camera camera,
                       BasisOfLCS lcsBasis, Translator translator) {
        super("Set coordinate system");
        this.videoPanel = videoPanel;
        this.setRobotPositionAction = setRobotPositionAction;
        this.camera = camera;
        this.lcsBasis = lcsBasis;
        this.translator = translator;
    }

    public void actionPerformed(ActionEvent e) {
        this.setEnabled(false);
        videoPanel.addMouseListener(new MouseAdapter() {
            private int clickCounter = 0;
            private ArrayList<Point2D> basis = new ArrayList<Point2D>();

            @Override
            public void mousePressed(MouseEvent e) {
                basis.add(videoPanel.getScaledPoint(new Point2D.Double(e.getX(), e.getY())));
                videoPanel.setCSBasis(basis);
                clickCounter++;
                if (clickCounter >= 4) {
                    videoPanel.removeMouseListener(this);
                    setRobotPositionAction.setEnabled(true);
                    lcsBasis = BasisOfLCS.createBasisOfLCSFromList(basis);
                    translator.setBasis(lcsBasis);
                    camera.setTranslator(translator);
                }
            }
        });
    }
}
